package com.lry.config;

import com.lry.config.condition.EaEnableCondition;
import com.lry.handler.ExceptionHandler;
import com.lry.properties.EaNoticeProperties;
import com.lry.web.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Configuration
@ConditionalOnWebApplication
@Conditional(EaEnableCondition.class)
@ConditionalOnProperty(value = "ea.notice.type",havingValue = "web",matchIfMissing = true)
public class EaWebTypeConfig  implements WebMvcConfigurer, WebMvcRegistrations {

    @Resource
    private ExceptionHandler exceptionHandler;

    @Resource
    private EaNoticeProperties eaNoticeProperties;

    @Override
    public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
        resolvers.add(0, exceptionHandlerResolver());
    }

    public EaHandlerExceptionResolver exceptionHandlerResolver() {
        EaHandlerExceptionResolver exceptionNoticeResolver = new EaHandlerExceptionResolver(exceptionHandler,
                currentRequetBodyResolver(), currentRequestHeaderResolver(), eaNoticeProperties);
        return exceptionNoticeResolver;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(clearBodyInterceptor());
    }

    @Bean
    public ClearBodyInterceptor clearBodyInterceptor() {
        ClearBodyInterceptor bodyInterceptor = new ClearBodyInterceptor(currentRequetBodyResolver());
        return bodyInterceptor;

    }

    @Bean
    @ConditionalOnMissingBean(value = CurrentRequestHeaderResolver.class)
    public CurrentRequestHeaderResolver currentRequestHeaderResolver() {
        return new DefaultRequestHeaderResolver();
    }

    @Bean
    public CurrentRequetBodyResolver currentRequetBodyResolver() {
        return new DefaultRequestBodyResolver();
    }

    @Override
    public RequestMappingHandlerAdapter getRequestMappingHandlerAdapter() {
        RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter();
        adapter.setRequestBodyAdvice(Arrays.asList(currentRequetBodyResolver()));
        return adapter;
    }


}
